package com.hyh.user.controller;

import com.hyh.user.domain.LoginForm;
import com.hyh.user.domain.User;
import com.hyh.user.result.PageResult;
import com.hyh.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*
     * @description: 发送短信验证码
     * @param:
     * @return:
     * @author
     * @date: 2024/3/18 15:19
     */
    @PostMapping ("/sendCode")
    public PageResult sendCode(@RequestParam("phone") String phone) {
        return userService.sendCode(phone);
    }

    /*
     * @description: 登录
     * @param:
     * @return:
     * @author
     * @date: 2024/3/18 15:18
     */
    @PostMapping ("/login")
    public PageResult login(@RequestBody User user) {
        return userService.login(user);
    }

    /*
     * @description: 验证码登录
     * @param:
     * @return:
     * @author
     * @date: 2024/3/18 16:02
     */
    @PostMapping ("/loginByCode")
    public PageResult loginByCode(@RequestBody LoginForm loginForm) {
        return userService.loginByCode(loginForm);
    }


    /*
     * @description: 修改自己的详细信息
     * @param:
     * @return:
     * @author
     * @date: 2024/3/18 18:59
     */
    @PutMapping ("/updateUser")
    public PageResult updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /*
     * @description: 根据id查询用户
     * @param:
     * @return:
     * @date: 2024/3/18 19:00
     */
    @GetMapping ("/getUserById/{id}")
    public PageResult getUserById(@PathVariable("id") Integer id) {
        return userService.getUserById(id);
    }

    /*
     * @description: 查询全部用户
     * @param:
     * @return:
     * @date: 2024/3/18 19:00
     */
    @GetMapping ("/getAllUser")
    @PreAuthorize("hasAuthority('sys:manage:all')")
    public PageResult getAllUser() {
        return userService.getAllUser();
    }


    /*
     * @description: 管理员重置密码
     * @param:
     * @return:
     * @author
     * @date: 2024/3/18 19:26
     */
    @PutMapping ("/resetPassword")
    @PreAuthorize("hasAuthority('sys:manage:all')")
    public PageResult resetPassword(@RequestBody User user) {
        return userService.resetPassword(user);
    }


    /*
     * @description: 上传头像
     * @param:
     * @return:
     * @author
     * @date: 2024/3/25 19:04
     */
    @PostMapping("/uploadAvatar/{id}")
    public PageResult uploadAvatar(@PathVariable("id") Long id,@RequestParam("file") MultipartFile file) {
        return userService.uploadAvatar(file,id);
    }

    /*测试redis工具类 此接口不做功能应用*/
    @GetMapping("/testRedisUtil/{id}")
    public PageResult testRedisUtil(@PathVariable("id") Long id) {
        return userService.testRedisUtil(id);
    }
}
